CREATE TABLE [dbo].[tb_Balances]
(
[idBalance] [int] NOT NULL IDENTITY(1, 1),
[Descripcion] [varchar] (250) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Fecha] [datetime] NOT NULL,
[Desde] [datetime] NOT NULL,
[Hasta] [datetime] NOT NULL,
[Cuenta] [int] NULL,
[SaldoAnterior] [float] NULL,
[Balance] [float] NULL,
[SaldoFinal] [float] NULL,
[FechaAprobacion] [datetime] NULL,
[Observaciones] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE TRIGGER [dbo].[updatetb_Balances] ON [dbo].[tb_Balances]
 FOR UPDATE AS 
 IF (update(idBalance)) begin 
	 SELECT * FROM inserted 
	 IF @@ROWCOUNT = 1 begin 
		 UPDATE tb_RegistroContable
		 SET Balance = (SELECT idBalance FROM inserted)
		 WHERE tb_RegistroContable.Balance = (SELECT idBalance FROM deleted)
	end  ELSE begin 
		 ROLLBACK TRANSACTION 
 end 
 end
GO
ALTER TABLE [dbo].[tb_Balances] ADD CONSTRAINT [PK_tb_Balances] PRIMARY KEY NONCLUSTERED  ([idBalance]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tb_Balances] WITH NOCHECK ADD CONSTRAINT [FK_tb_Balances_tb_Cuentas1] FOREIGN KEY ([Cuenta]) REFERENCES [dbo].[tb_Cuentas] ([idCuenta])
GO
